<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form
      :inline="true"
      class="demo-form-inline"
    >
      <div>
        <el-form-item>
          <el-input
            class="long"
            v-model="searchObj.title"
            placeholder="标题"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            class="long"
            v-model="searchObj.advantage"
            placeholder="描述"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            class="long"
            v-model="searchObj.address"
            placeholder="地址"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            class="short"
            v-model="searchObj.typeId"
            placeholder="类型"
          >
            <el-option
              v-for="item in options"
              clearable
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div>
        <el-form-item>
          <el-input
            class="short"
            v-model="searchObj.priceMin"
            placeholder="最低房租"
          />
          <span class="wave">~</span>
          <el-input
            class="short"
            v-model="searchObj.priceMax"
            placeholder="最高房租"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            class="short"
            v-model="searchObj.areaMin"
            placeholder="最小面积"
          />
          <span class="wave">~</span>
          <el-input
            class="short"
            v-model="searchObj.areaMax"
            placeholder="最大面积"
          />
        </el-form-item>
        <el-form-item>        
          <el-date-picker
             v-model="searchObj.houseDate"
             type="date"
             placeholder="发布日期"
            value-format="yyyy-MM-dd"
             style="width:140px"
          />
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="fetchData()"
        >查询</el-button>
        <el-button
          type="default"
          @click="resetData()"
        >清空</el-button>    
      </div>
    </el-form>

    <!-- :data 用于存放请求数据回来的数组 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-lodaing-text="数据加载中"
      fit
      border
      highlight-current-row
      stripe
    >
      <el-table-column
        prop="isReviewed"
        label="审核状态"
        min-width="8%"
        align="center"
      >
        <template scope="scope">
          <div v-if="scope.row.isReviewed == '0'">
            <el-tag
              type="info"
              effect="dark"
            ><i class="el-icon-question tag"/>待审核</el-tag>
          </div>
          <div v-if="scope.row.isReviewed == '1'">
            <el-tag
              type="success"
              effect="dark"
            ><i class="el-icon-success tag"/>通过</el-tag>
          </div>
          <div v-if="scope.row.isReviewed == '2'">
            <el-tag
              type="warning"
              effect="dark"
            ><i class="el-icon-remove tag"/>未通过</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="houseId"
        label="ID"
        min-width="5%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        min-width="17%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="advantage"
        label="描述"
        min-width="12%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="price"
        :label="'房租\n(￥/月)'"
        min-width="6%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="area"
        :label="'面积\n(㎡)'"
        min-width="6%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        min-width="12%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="typeId"
        label="类型"
        min-width="8%"
        align="center"
      >
        <template scope="scope">
          <div v-if="scope.row.typeId == '0'">
            <el-tag
              type="success"
              effect="plain"
            >新房</el-tag>
          </div>
          <div v-if="scope.row.typeId == '1'">
            <el-tag
              type="warning"
              effect="plain"
            >二手房</el-tag>
          </div>
          <div v-if="scope.row.typeId == '2'">
            <el-tag
              type="primary"
              effect="plain"
            >商业租房</el-tag>
          </div>
          <div v-if="scope.row.typeId == '100'">
            <el-tag
              type="info"
              effect="plain"
            >其他</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="houseDate"
        label="发布日期"
        min-width="8%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="userId"
        :label="'用户\nID'"
        min-width="5%"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="15%"
        align="center"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              plain
              @click="toEdit(scope.row.houseId)"
            >修改</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              plain
              @click="removeDataById(scope.row.houseId)"
            >删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->    
    <el-pagination
      background
      :hide-on-single-page="true"
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import house from '@/api/house';
export default {
  // 定义数据
  data() {
    return {
      listLoading: true, // 是否显示loading信息
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件
      options: [
        {
          value: 0,
          label: '新房'
        },
        {
          value: 1,
          label: '二手房'
        },
        {
          value: 3,
          label: '商业租房'
        }
      ]
    };
  },
  // 当前页面加载时获取数据
  created() {
    this.fetchData();
  },
  // 调用api层获取数据库中的数据
  methods: {
    // 页面跳转
    toEdit(id) {
      this.$router.push('/house/edit/' + id);
    },
    // 获取数据
    fetchData(page = 1) {
      this.page = page;
      this.listLoading = true;
      house
        .getHouseListByPage(this.page, this.limit, this.searchObj)
        // 请求成功
        .then(response => {
          if (response.success === true) {
            this.list = response.data.items;
            this.total = response.data.total;
          }
          this.listLoading = false;
        })
        // 请求失败
        .catch(() => {
          this.$message({
            type: 'error',
            message: '获取数据失败'
          });
        });
    },
    // 清空
    resetData() {
      this.searchObj = {};
      this.fetchData();
    },
    // 删除数据
    removeDataById(id) {
      this.$confirm('此操作将永久删除该记录，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        // 点击确定执行then()
        .then(() => {
          return house.removeHouseById(id);
        })
        .then(() => {
          this.fetchData();
          this.$message({
            type: 'success',
            message: '删除成功'
          });
        })
        // 点击取消执行catch()
        .catch(response => {
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          } else {
            this.$message({
              type: 'error',
              message: '删除失败'
            });
          }
        });
    }
  }
};
</script>

<style>
.el-table th.gutter {
  display: table-cell !important;
}
.el-table .cell {
  white-space: pre-line;
}
.wave {
  padding: 0 5px;
  font-weight: 600;
}
.long {
  width: 215px;
}
.short {
  width: 100px;
}
.tag {
  padding-right: 5px;
}
</style>